#include "dialog.h"
#include "ui_dialog.h"
#include <QMouseEvent>
void Dialog::resetCurrentSeconds()
{
    currentSeconds = expectedSeconds;
    updateNumber();
}

void Dialog::setBackgroundColor(const QString color)
{
//    setAutoFillBackground(true);
//    QPalette p = this->palette();
//    p.setColor(QPalette::Window, color);
//    setPalette(p);
    ui->lcdNumber->setStyleSheet(tr("color:%1").arg(color));
}

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog),
    timer(new QTimer)
{
    ui->setupUi(this);
    initUi();
    connectSlot();
}

void Dialog::connectSlot()
{
    connect(ui->btnStart, SIGNAL(clicked()), this, SLOT(switchStart()));
    connect(ui->btnLoop, SIGNAL(clicked()), this, SLOT(switchLoop()));
    connect(ui->btnDirection, SIGNAL(clicked()), this, SLOT(switchDirection()));
    connect(ui->btnQuit, SIGNAL(clicked()), this, SLOT(close()));
    connect(ui->editSeconds, SIGNAL(valueChanged(int)), this, SLOT(modifyExpectedSeconds(int)));
    connect(timer, SIGNAL(timeout()), this, SLOT(updateSeconds()));
    connect(ui->btnRestart, SIGNAL(clicked()), this, SLOT(restart()));
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        distance = e->globalPosition() - this->pos();
        e->accept();
        isDrag = true;
    }

}

void Dialog::mouseMoveEvent(QMouseEvent *e)
{
    if (isDrag && (e->buttons() == Qt::LeftButton)) {
            this->move((e->globalPosition() - distance).toPoint());
            e->accept();
        }
}

void Dialog::mouseReleaseEvent(QMouseEvent *)
{
    isDrag = false;
}

void Dialog::warn(QWidget * w)
{
//    w->setStyleSheet("background: rgb(23, 74, 129)");
   w->setStyleSheet("background:#993333");
}

void Dialog::succ(QWidget * w)
{
    w->setStyleSheet("background:green");
}

void Dialog::showStart()
{
    isStart = true;
    isZero = false;
    succ(ui->btnStart);
//    ui->btnStart->setText("已开启");
}

void Dialog::showStop()
{
    isStart = false;
//    ui->btnStart->setText("已停止");
    warn(ui->btnStart);
}

void Dialog::initUi()
{
    ui->editSeconds->setMaximum(9999);
    ui->lcdNumber->setDigitCount(4);
    showStop();
    isDrag = false;
    isCountdown = true;
    again = true;
    timer->setInterval(1000);
    expectedSeconds = 60;
    resetCurrentSeconds();
    ui->editSeconds->setValue(expectedSeconds);
    updateNumber();
    move(0,0);
    setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
}

void Dialog::updateNumber()
{
    if (isZero) {
        setBackgroundColor("#7e1507");
    }else {
        setBackgroundColor("rgb(108, 253, 255);");
    }
    ui->lcdNumber->display(currentSeconds);

}

void Dialog::updateSeconds()
{
    if (isCountdown) {
        --currentSeconds;
    }else {
        ++currentSeconds;
    }
    if (currentSeconds <= 0) {
        isZero = true;
        showStop();
    }
    if (currentSeconds < 0) {
        if (again) {
           resetCurrentSeconds();
           showStart();
            updateNumber();
        }else {
            timer->stop();
        }
    }else {
        updateNumber();
    }

}

void Dialog::switchStart()
{
    if (isStart) {
        showStop();
        timer->stop();
    }else {
        showStart();
        if (currentSeconds <= 0) {
            resetCurrentSeconds();
        }
        timer->start();
    }
}

void Dialog::switchDirection()
{
    isCountdown = !isCountdown;
    if (isCountdown) {
        timer->stop();
        ui->btnDirection->setText(tr("倒计时"));
        resetCurrentSeconds();
        updateNumber();
        showStart();
        timer->start();
    }else {
        timer->stop();
        currentSeconds = 0;
        showStart();
        updateNumber();
        timer->start();
        ui->btnDirection->setText(tr("正计时"));
    }
}

void Dialog::modifyExpectedSeconds(int seconds)
{
    expectedSeconds = currentSeconds = seconds;
}

void Dialog::switchLoop()
{
    again = !again;
    if (again) {
        ui->btnLoop->setText(tr("循 环"));
    }else {
        ui->btnLoop->setText(tr("单 次"));
    }
}

void Dialog::restart()
{
    timer->stop();
    showStart();

    if (isCountdown) {
        resetCurrentSeconds();
    }else {
        currentSeconds = 0;
    }
    updateNumber();
    timer->start();
}
